<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>binomial</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : 26/09/2003</div>
    <p>
      <b>binomial</b> -  binomial distribution probabilities</p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>pr=binomial(p,n)  </tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>pr</b>
        </tt>: row vector with n+1 components</li>
      <li>
        <tt>
          <b>p</b>
        </tt>: real number in [0,1]</li>
      <li>
        <tt>
          <b>n</b>
        </tt>: an integer &gt;= 1</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
      <tt>
        <b>pr=binomial(p,n)</b>
      </tt> returns the binomial probability vector, i.e. 
    <tt>
        <b>pr(k+1)</b>
      </tt> is the probability of <tt>
        <b>k</b>
      </tt> success in <tt>
        <b>n</b>
      </tt> independent 
    Bernouilli trials with probability of success <tt>
        <b>p</b>
      </tt>. In other words :
    <tt>
        <b>pr(k+1) = probability(X=k)</b>
      </tt> , with X a random variable following the B(n,p) 
    distribution, and numerically :</p>
    <pre>

            / n \  k      n-k       / n \       n!
 pr(k+1) =  |   | p  (1-p)    with  |   |  = ---------
            \ k /                   \ k /    k! (n-k)!
   
    </pre>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>

// first example
n=10;p=0.3; xbasc(); plot2d3(0:n,binomial(p,n));

// second example 
n=50;p=0.4;
mea=n*p; sigma=sqrt(n*p*(1-p));
x=( (0:n)-mea )/sigma;
xbasc()
plot2d(x, sigma*binomial(p,n));
deff('y=Gauss(x)','y=1/sqrt(2*%pi)*exp(-(x.^2)/2)')
plot2d(x, Gauss(x), style=2);

// by binomial formula (Caution if big n)
function pr=binomial2(p,n)
x=poly(0,'x');pr=coeff((1-p+x)^n).*horner(x^(0:n),p);
endfunction
p=1/3;n=5;
binomial(p,n)-binomial2(p,n)

// by Gamma function: gamma(n+1)=n! (Caution if big n)
p=1/3;n=5;
Cnks=gamma(n+1)./(gamma(1:n+1).*gamma(n+1:-1:1));
x=poly(0,'x');
pr=Cnks.*horner(x.^(0:n).*(1-x)^(n:-1:0),p);
pr-binomial(p,n)
 
  </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="../dcd/cdfbin.htm">
        <tt>
          <b>cdfbin</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="../dcd/grand.htm">
        <tt>
          <b>grand</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
  </body>
</html>
